package com.victor.zjbj_android.base;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;
import com.victor.zjbj_android.R;

import java.util.ArrayList;

public class ImageDetailActivity extends AppCompatActivity {


    public static final String IMG_PATH_LIST = "img_path_list";
    public static final String CURRENT_CLICK_POS = "current_click_pos";

    public static void actionStart(Activity host, ArrayList<String> imgPathList, int clickPos) {
        Intent intent = new Intent(host, ImageDetailActivity.class);
        intent.putExtra(CURRENT_CLICK_POS, clickPos);
        intent.putStringArrayListExtra(IMG_PATH_LIST, imgPathList);
        host.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_detail);
        ViewPager imgDetailPager = findViewById(R.id.vp_images);
        final TextView tvCurrent = findViewById(R.id.tv_current);
        TextView tvTotalNumber = findViewById(R.id.tv_total_number);

        ArrayList<String> imgPathList = getIntent().getStringArrayListExtra(IMG_PATH_LIST);
        int currentPos = getIntent().getIntExtra(CURRENT_CLICK_POS, 0);
        if (imgPathList == null || imgPathList.isEmpty()) {
            return;
        }

        ImageDetailAdapter imageDetailAdapter = new ImageDetailAdapter(imgPathList);
        imgDetailPager.setAdapter(imageDetailAdapter);
        imgDetailPager.setCurrentItem(currentPos);

        tvCurrent.setText(String.valueOf(currentPos + 1));
        tvTotalNumber.setText(String.valueOf(imgPathList.size()));

        imgDetailPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tvCurrent.setText(String.valueOf(position + 1));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    //vp填充
    private class ImageDetailAdapter extends PagerAdapter {

        private ArrayList<String> imgPathList;

        private ImageDetailAdapter(ArrayList<String> imgPathList) {
            this.imgPathList = imgPathList;
        }

        @Override
        public int getCount() {
            return imgPathList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            PhotoView view = new PhotoView(container.getContext());

            view.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
            view.setScaleType(ImageView.ScaleType.FIT_CENTER);

            String imgPath = imgPathList.get(position);
//            if (imgPath.startsWith(NetConstantUtils.IMG_BASE_URL)) {
//                Glide.with(container.getContext()).load(NetConstantUtils.IMG_BASE_URL+imgPath).into(view);
//            } else {
////                Uri uri = Uri.fromFile(new File(imgPath));
//                Glide.with(container.getContext()).load(NetConstantUtils.IMG_BASE_URL+imgPath).into(view);
//            }
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ImageDetailActivity.this.finish();
                }
            });
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
